package com.thti.mecool.client.model.biz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.threeti.mecool.mobile.interfaces.dto.ChannelDto;
import com.threeti.mecool.mobile.interfaces.dto.MemoDto;
import com.thti.mecool.client.db.DataBase;
/**
 * 业务bean
 *
 */
public class WorkmemoBizImpl {
	private DataBase dataBase;

	public WorkmemoBizImpl(Context context) {
		dataBase = new DataBase(context);
	}
	public void save(MemoDto memoDto,int curpage){
		String sqlString="REPLACE INTO workmemo(userId ,memoId,channelCode,content,channelName,recordDate,page) values(?,?,?,?,?,?,?)";
		dataBase.execObject(sqlString, new String[]{memoDto.getUserId(),memoDto.getMemoId(),memoDto.getChannelCode(),memoDto.getContent(),memoDto.getChannelName(),memoDto.getRecordDate(),String.valueOf(curpage)});
		dataBase.close();
	}	
	public void saveAll(List<MemoDto> memoDtos,int curpage){
		String sqlString="REPLACE INTO workmemo(userId ,memoId,channelCode,content,channelName,recordDate,page) values(?,?,?,?,?,?,?)";
		ArrayList<HashMap<String, String[]>> args=new ArrayList<HashMap<String,String[]>>();
		for(MemoDto memoDto:memoDtos){
			HashMap<String, String[]> map=new HashMap<String, String[]>();
			map.put(sqlString, new String[]{memoDto.getUserId(),memoDto.getMemoId(),memoDto.getChannelCode(),memoDto.getContent(),memoDto.getChannelName(),memoDto.getRecordDate(),String.valueOf(curpage)});
			args.add(map);
		}
		dataBase.execTransaction(args);
//		dataBase.execObject(sqlString, new String[]{memoDto.getUserId(),memoDto.getMemoId(),memoDto.getChannelCode(),memoDto.getContent(),memoDto.getChannelName(),memoDto.getRecordDate(),String.valueOf(curpage)});
		dataBase.close();
	}
	public void updateMemoDto(MemoDto memoDto){
		String sql="update workmemo set channelCode=?,channelName=?,content=? where memoId=? AND userId=?;";
		dataBase.execObject(sql, new String[]{memoDto.getChannelCode(),memoDto.getChannelName(),memoDto.getContent(),memoDto.getMemoId(),memoDto.getUserId()});
	}
	public void updateChannelName(String channelName,String memoId,String channelCode ){
		String sqlString="update workmemo set channelName=? where memoId=? AND channelCode=?";
		dataBase.execObject(sqlString, new String[]{channelName,memoId,channelCode});
		dataBase.close();
	}
	
	public String findChannelName(String channelCode){
		String sql="select name from workmemoChannel where code=?";
		Cursor cursor=dataBase.getWritableDatabase().rawQuery(sql, new String[]{channelCode});
		String channelName=null;
		if(cursor.moveToNext())
			channelName=cursor.getString(0);
		dataBase.close();
		return channelName;
	}
	
	public void delete(String  memoid ){
		String sql="delete from workmemo where memoid=?";
		dataBase.execObject(sql, new String[]{memoid});
		dataBase.close();
	}
	
	public void deleteTable(String tableName){
		dataBase.getWritableDatabase().delete(tableName, null, null);
		dataBase.execObject("update sqlite_sequence set seq=0 where name=?", new String[]{tableName});
		dataBase.close();
	}
	public void saveChannel(ChannelDto channelDto){
		String sqlString="insert into workmemoChannel(code,name) values(?,?)";
		dataBase.execObject(sqlString, new String[]{channelDto.getCode(),channelDto.getName()});
		dataBase.close();
	}
	@SuppressWarnings("unchecked")
	public List<com.thti.mecool.client.protocol.work.MemoDto> findAll(int curpage){
		String sql="select * from  workmemo where page=?";
		List<com.thti.mecool.client.protocol.work.MemoDto>	list=dataBase.findAll(com.thti.mecool.client.protocol.work.MemoDto.class, sql, new String[]{String.valueOf(curpage)});
		return list==null?new ArrayList<com.thti.mecool.client.protocol.work.MemoDto>():list;
	}
	@SuppressWarnings("unchecked")
	public HashMap<String ,String> findChannelAll(){
		String sql="select * from  workmemoChannel";
		HashMap<String,String>	ChannelDtos=new HashMap<String, String>();
		List<ChannelDto> list=dataBase.findAll(ChannelDto.class, sql, new String[]{});
		for(ChannelDto c:list){
			ChannelDtos.put(c.getCode(),c.getName());
		}
		return ChannelDtos==null?new HashMap<String,String>():ChannelDtos;
	}
	
}
